<?php
defined('APP_PATH') or die ('Unauthorized access');

/**
 * 视图核心类库
 * @author windpurple
 * @version 1.0
 */
defined('APP_PATH') or die ('Unauthorized access');

final class View
{
	private $controller;
	private $action;
	private $view_file;
	private $view_filepath;
	private $view_suffix;
	static private $_instance = NULL;
	
	private function __construct()
	{
		
	}
	
	private function __clone()
	{
		
	}
	
	
	public function __set($name,$value)
	{
		$this->$name = $value;
	}
	
	public function __get($name)
	{
		return $this->$name;
	}
	
	static public function getInstance()
	{
		if(is_null(self::$_instance) || !isset(self::$_instance)){
			self::$_instance = new View();
		}
		return self::$_instance;
	}
	
	//显示
	final public function display($template = '',$default = true)
	{
		global $view;
		global $config;
		$view = $this;
		$file = $this->getView($template);
		
		//缓存
		if($config['cache_time'] > 0){
			$cache_dir = APP_PATH.'/'.$config['cache_dir'];
			$cache_file = strtoupper(md5(md5($file.'_'.$_SERVER['REQUEST_URI']))).'.'.$config['cache_suffix'];
			
			if(!is_writable($cache_dir)) Error::message($cache_dir.' 不可写');
			
			if(file_exists($cache_dir.'/'.$cache_file)){	
				
				if(filectime($cache_dir.'/'.$cache_file) < time() - $config['cache_time']){
					ob_start();
					include_once($file);
					$content = ob_get_contents();
					ob_end_clean();
					
					$file = fopen($cache_dir.'/'.$cache_file,'w');
					fwrite($file,$content);
					fclose($file);
					echo $content;
				}else{
					$content = file_get_contents($cache_dir.'/'.$cache_file);
					echo $content;
				}
			}else{
				ob_start();
				include_once($file);
				$content = ob_get_contents();
				ob_end_clean();
				
				$file = fopen($cache_dir.'/'.$cache_file,'w');
				fwrite($file,$content);
				fclose($file);
				echo $content;
			}
			
			
		}else{
			ob_start();
			include_once($file);
			$content = ob_get_contents();
			ob_end_clean();
			echo $content;
		}
		
		if($default){
			exit;
		}
		
	}
	

	
	/**
	 * 获取视图文件
	 */
	final public function getView($template = '')
	{
		if(empty($template)){
			$file = $this->view_file;
		}else{
			$file = explode('/',$template);
			if(count($file) >= 2){
				$file = $this->view_filepath.'../'.$template.'.'.$this->view_suffix;
			}else{
				$file = $this->view_filepath.$template.'.'.$this->view_suffix;
			}
		}
		//检查视图文件是否存在
		if(!file_exists($file)){
			Error::message(str_replace($this->view_filepath,'',$file).' 视图文件不存在');
		}
		
		return $file;
	}
	
	/**
	 * 视图地址解析
	 * @param $params
	 */
	private function url($params = array())
	{
		$url = '';
		$root_app_path = '/';
		if(IMPORT_PATH != realpath($_SERVER['DOCUMENT_ROOT'])){
			$root_app_path = str_replace(realpath($_SERVER['DOCUMENT_ROOT']),'',IMPORT_PATH).'/';
		}
		if(is_array($params)){
			global $config;
			switch($config['url_type']){
				case '1':
					$url = $root_app_path.'?';
					foreach($params as $k => $v){
						$url .= $k .'='.$v.'&';
					}
				break;
				case '2':
					$url = $root_app_path;
					foreach($params as $k => $v){
						$url .= $k.'/'.$v.'/';
					}
				break;
				default:
					$url = $root_app_path.'?';
					foreach($params as $k => $v){
						$url .= $k .'='.$v.'&';
					}
					
				break;
			}
			$url = substr($url,0,-1);
		}else{
			$url = $params;
		}
		return $url;
	}
	
	
}
?>